package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;

import com.sky.service.common.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @ApiOperation("新增分类")
    public Result save(@RequestBody CategoryDTO dto) {
        int rows = categoryService.save(dto);
        return rows > 0 ? Result.success() : Result.error("新增分类失败");
    }


    @PutMapping
    @ApiOperation("修改分类")
    public Result update(@RequestBody CategoryDTO dto) {
        int rows = categoryService.update(dto);
        return rows > 0 ? Result.success() : Result.error("修改分类失败");
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> pageQuery(CategoryPageQueryDTO pagedto){
        PageResult pageResult = categoryService.pageQuery(pagedto);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("删除分类")
    public Result delete(Long id) {
        int rows = categoryService.delete(id);
        return rows > 0 ? Result.success() : Result.error("删除分类失败");
    }


    @GetMapping("/list")
    @ApiOperation("查询分类")
    public Result<List<Category>> findByType(Integer type){
        List<Category> list = categoryService.findByType(type);
        return Result.success(list);

    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用分类")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        int rows = categoryService.StartOrStop(status, id);
        return rows > 0 ? Result.success() : Result.error("操作失败");
    }


}
